<?php
namespace PFBC\View;

class ModalDataTable extends \PFBC\View {
	protected $class = "form-horizontal";

	public function render() {
		$this->_form->appendAttribute("class", $this->class);

		echo '<form', $this->_form->getAttributes(), '><fieldset>';
		$this->_form->getErrorView()->render();

		$elements = $this->_form->getElements();
		$elementSize = sizeof($elements);
		$elementCount = 0;
		$submitCount = 1;
		
		$formID = $this->_form->getAttribute("id");
		
		for($e = 0; $e < $elementSize; ++$e) {
			$element = $elements[$e];

			if ( $element instanceof \PFBC\Element\ModalHeading ) {
				echo '<div id="' . $formID . 'Modal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="' . $formID . 'ModalLabel" aria-hidden="true">
						  <div class="modal-header">
							  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
							   ', $element->render(), '
						  </div>
						  <div class="modal-body">';
			} elseif ( $element instanceof \PFBC\Element\Hidden || $element instanceof \PFBC\Element\HTML || $element instanceof \PFBC\Element\Legend || $element instanceof \PFBC\Element\ModalToggle ) {
				$element->render();
			} elseif ( $element instanceof \PFBC\Element\Button ) {
				if ( $submitCount == 1 ) {
				echo '   </div>
						  <div class="modal-footer">
							  <button id="' . $formID . '_close" class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
							  ', $element->render();
				} elseif ( $submitCount == 2 ) {
					$element->render();
				} elseif ( $submitCount == 3 ) {
					$element->render();
					echo '
						  </div>
					  </div>';
				}
				$submitCount++;
            } else {
				echo '<div class="control-group">', $this->renderLabel($element), '<div class="controls">', $element->render(), $this->renderDescriptions($element), '</div></div>';
				++$elementCount;
			}
		}

		echo '</fieldset></form>';
		// Add JS for View
		?>
		<script type="text/javascript">
			nForm = jQuery('#<?php echo $formID; ?>');
			nFormTask = jQuery('input[name="web_apper_task"]', nForm);
			nModal = jQuery('.modal', nForm);
			nModalHeader = jQuery('.modal-header h3', nModal)
			nModalBody = jQuery('.modal-body', nModal);
			nModalClose = jQuery('.modal-footer [data-dismiss="modal"]', nModal)
			nModalSaveBtn = jQuery('.modal-footer #save', nModal)
			nModalUpdateBtn = jQuery('.modal-footer #update', nModal)
			nModalDeleteBtn = jQuery('.modal-footer #delete', nModal)
			nModalElements = jQuery('.modal-body>*', nModal);
			nDeleteWarning = jQuery('#deleteWarning', nModal);
			nTable = jQuery('#<?php echo $formID; ?>_datatable');
	
			jQuery(document).ready(function($) {

				// Add a click handler to the rows - Set selected row class
				oTable.on("click", "tr", function( e ) {
					if ( $(this).hasClass('row_selected') ) {
						$(this).removeClass('row_selected');
					} else {
						oTable.$('tr.row_selected').removeClass('row_selected');
						$(this).addClass('row_selected');
					}
				});

				// Add a click handler for the Editor Add row button
				$('#add_<?php echo $formID; ?>_datatable').on("click", function() {
					$('.alert').remove();  // Close any alerts that may be open
					nForm.clearForm();  // Clear form values
					nFormTask.val('add');  // Set value of hidden form field to desired action
					nModalHeader.html('New');  // Change the modal header
					nModalElements.css('display', 'block');  // Show all elements in the modal body, &
					  nDeleteWarning.css('display', 'none');  // hide the delete warning.
					nModalClose.html('Cancel');  // Relabel modal close button
					nModalSaveBtn.css('display', 'inline-block');  // Show the Save button, &
					  nModalUpdateBtn.css('display', 'none');  // hide the Update button, &
					  nModalDeleteBtn.css('display', 'none');  // hide the Delete button.
					nModal.modal({ keyboard: false });  // Show the modal
				});

				// Add a click handler for the Editor Edit row button
				$('#edit_<?php echo $formID; ?>_datatable').on("click", function() {
					if ( $('tr.row_selected').length === 0 )
						return false;
					$('.alert').remove();  // Close any alerts that may be open
					nForm.clearForm();  // Clear form values
					var rowValues = oTable.fnGetData( $('tr.row_selected')[0] );  // Get array of values from dataTable row
					nForm.setFormValues(rowValues);  // Set form values with array collected from dataTable 
					nFormTask.val('update');  // Set value of hidden form field to desired action
					nModalHeader.html('Update');  // Change the modal header
					nModalElements.css('display', 'block');  // Show all elements in the modal body, &
					  nDeleteWarning.css('display', 'none');  // hide the delete warning.
					nModalClose.html('Cancel');  // Relabel modal close button
					nModalUpdateBtn.css('display', 'inline-block');  // Show the Update button, &
					  nModalSaveBtn.css('display', 'none');  // hide the Save button, &
					  nModalDeleteBtn.css('display', 'none');  // hide the Delete button.
					nModal.modal({ keyboard: false });  // Show the modal
				});

				// Add a click handler for the Editor Delete row button 
				$('#delete_<?php echo $formID; ?>_datatable').on("click", function() {
					if ( $('tr.row_selected').length === 0 )
						return false;
					$('.alert').remove();  // Close any alerts that may be open
					nForm.clearForm();  // Clear form values
					var rowValues = oTable.fnGetData( $('tr.row_selected')[0] );  // Get array of values from dataTable row
					nForm.setFormValues(rowValues);  // Set form values with array collected from dataTable 
					nFormTask.val('delete');  // Set value of hidden form field to desired action
					nModalHeader.html('Delete');  // Change the modal header
					nModalElements.css('display', 'none');  // Hide all elements in the modal body, &
					  nDeleteWarning.css('display', 'block');  // show the delete warning.
					nModalClose.html('Cancel');  // Relabel modal close button
					nModalDeleteBtn.css('display', 'inline-block');  // Show the Delete button, &
					  nModalSaveBtn.css('display', 'none');  // hide the Save button, &
					  nModalUpdateBtn.css('display', 'none');  // hide the Update button.
					nModal.modal({ keyboard: false });  // Show the modal
				});

				// Add a click handlers for the form submit buttons
				nForm.on('submit', function() {
					nModalSaveBtn.button('loading') ; // Give modal submit button a loading state
					nModalUpdateBtn.button('loading') ; // Give modal submit button a loading state
					nModalDeleteBtn.button('loading') ; // Give modal submit button a loading state
					$('.alert').alert('close');  // Close any alerts that may be open
				});

			});
			
			function resetModalButtons() {
				nModalSaveBtn.button('reset');  // Remove loading state from modal submit button
				nModalUpdateBtn.button('reset');  // Remove loading state from modal submit button
				nModalDeleteBtn.button('reset');  // Remove loading state from modal submit button
				nModalClose.html('Close');  // Relabel modal close button
			}

        </script>
        <?php
    }

	protected function renderLabel(\PFBC\Element $element) {
        $label = $element->getLabel();
        if(!empty($label)) {
			echo '<label class="control-label" for="', $element->getAttribute("id"), '">';
			if($element->isRequired())
				echo '<span class="required">* </span>';
			echo $label, '</label>'; 
        }
    }
}	
